package com.hnqcgc.fz.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MyConnection extends IConnection{
    private String Url="jdbc:mysql://localhost:3306/dujiaoshou?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true";
    private String Username="root";
    private String Pwd="9572457";

    private static com.hnqcgc.fz.db.MyConnection myConnection=new com.hnqcgc.fz.db.MyConnection();//单例模式

    public static com.hnqcgc.fz.db.MyConnection getInstance(){

        return myConnection;
    }
    static {
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //数据库连接方法
    @Override
    public Connection getConn() throws SQLException {
        Connection connection = DriverManager.getConnection(Url,Username,Pwd);


        return connection;
    }

    public static void main(String[] args) {
        //测试数据库是否连接成功
        Connection connection=null;
        try {
            connection=myConnection.getConn();
            System.out.println(connection.isClosed());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
